import os

import pytest
from errors import Duplicate, Missing
from model.explorer import Explorer

# set this before data imports below for data.init
os.environ["CRYPTID_SQLITE_DB"] = ":memory:"
from data import explorer


@pytest.fixture
def sample() -> Explorer:
    return Explorer(
        name="Alexander Golde",
        country="RU",
        description="Scarce during full moons",
    )


#
# These tests will be executed sequentially in the same order each time.
#
def test_create(sample):
    resp = explorer.create(sample)
    assert resp == sample


def test_create_duplicate(sample):
    with pytest.raises(Duplicate):
        explorer.create(sample)


def test_get_one(sample):
    resp = explorer.get_one(sample.name)
    assert resp == sample


def test_get_one_missing():
    with pytest.raises(Missing):
        explorer.get_one("hazadus")


def test_modify(sample):
    modified = Explorer(
        name=sample.name,
        country=sample.country,
        description=sample.description,
    )
    modified.description = "Mad lad"
    resp = explorer.modify(sample.name, modified)
    assert resp == modified


def test_modify_missing():
    guy: Explorer = Explorer(name="Alex Gold", country="RU", description="Mad lad")
    with pytest.raises(Missing):
        explorer.modify(guy.name, guy)


def test_delete(sample):
    resp = explorer.delete(sample.name)
    assert resp is None


def test_delete_missing(sample):
    with pytest.raises(Missing):
        explorer.delete(sample.name)
